<?php use_helper('I18N', 'Date') ?>
<?php use_helper('Javascript') ?>
<?php include_partial('ingresoReparaciones/assets') ?>

<div id="sf_admin_container">
  <h1><?php echo __('Asistente de Ingreso a Reparación', array(), 'messages') ?></h1>

  <?php include_partial('ingresoReparaciones/flashes') ?>

  <div id="sf_admin_header">
    <?php include_partial('ingresoReparaciones/form_header', array('ordenesservicio' => $ordenesservicio, 'form' => $form, 'configuration' => $configuration)) ?>
  </div>

  <div id="sf_admin_content">
    <?php include_partial('ingresoReparaciones/form', array('ordenesservicio' => $ordenesservicio, 'form' => $form, 'configuration' => $configuration, 'helper' => $helper)) ?>
  </div>

  <div id="sf_admin_footer">
    <?php include_partial('ingresoReparaciones/form_footer', array('ordenesservicio' => $ordenesservicio, 'form' => $form, 'configuration' => $configuration)) ?>
  </div>
</div>

<!-- Agregado por Marcelo -->

<style type="text/css">
		/*body { font-size: 62.5%; }
		label, input { display:block; }*/
		input.text { margin-bottom:12px; width:95%; padding: .4em; }
		#autocomplete_ordenesservicio_clientes_id { margin-bottom:12px; width:30%; padding: .4em; }
                
                #clientes_clientestipo_id { margin-bottom:12px; width:95%; padding: .4em; }

                fieldset { padding:0; border:0; margin-top:25px; }
		h1 { font-size: 1.2em; margin: .6em 0; }
		div#users-contain { width: 350px; margin: 20px 0; }
		div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; }
		div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; }
		.ui-dialog .ui-state-error { padding: .3em; }
		.validateTips { border: 1px solid transparent; padding: 0.3em; }

	</style>
	<script type="text/javascript">
	jQuery(function() {
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
		jQuery("#dialog").dialog("destroy");

		var     nombre = jQuery("#clientes_nombre"),
		        apellido = jQuery("#clientes_apellido"),
		        ruc = jQuery("#clientes_ruc"),
                        tipo_cliente = jQuery("clientes_clientestipo_id"),
                        email = jQuery("#clientes_correo1"),
			direccion = jQuery("#clientes_direccion"),
			allFields = jQuery([]).add(nombre).add(apellido).add(ruc).add(tipo_cliente).add(email).add(direccion),
			tips = jQuery(".validateTips");

		function updateTips(t) {
			tips
				.text(t)
				.addClass('ui-state-highlight');
			setTimeout(function() {
				tips.removeClass('ui-state-highlight', 1500);
			}, 500);
		}

		function checkLength(o,n,min,max) {

			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("La longitud de " + n + " debe estar entre "+min+" y "+max+".");
				return false;
			} else {
				return true;
			}

		}

		function checkRegexp(o,regexp,n) {

			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}

		}

		jQuery("#dialog-form").dialog({
			autoOpen: false,
			height: 510,
			width: 350,
			modal: true,
			buttons: {
				'Crear Cliente': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');
					bValid = bValid && checkLength(nombre,"Nombre",3,25);
					bValid = bValid && checkLength(apellido,"Apellido",3,25);
					bValid = bValid && checkLength(ruc,"ruc",6,20);
					bValid = bValid && checkLength(email,"E-mail",6,50);
					bValid = bValid && checkLength(direccion,"Direccion",6,30);

					bValid = bValid && checkRegexp(nombre,/^[a-z]([a-z áéíóú])+$/i,"El Nombre consiste en letras de  a-z en mayusculas o minusculas.");
					bValid = bValid && checkRegexp(apellido,/^[a-z]([a-z áéíóú])+$/i,"El Apellido consiste en letras de  a-z en mayusculas o minusculas.");
					bValid = bValid && checkRegexp(ruc,/^[0-9]([0-9])+$/i,"El R.U.C consiste en numeros del 0-9 y puede contener Una Letra.");
					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. ui@jquery.com");
					bValid = bValid && checkRegexp(direccion,/^([0-9a-z A-Z])+$/,"El campo direccion acepta: a-z 0-9");

					if (bValid) {
						//jQuery('#users tbody').append('<tr>' +
						//	'<td>' + name.val() + '</td>' +
						//	'<td>' + email.val() + '</td>' +
						//	'<td>' + password.val() + '</td>' +
						//	'</tr>');
                                                //Agregado por Marcelo
                                                new Ajax.Updater('Actualizar_Ajax',
                                                "<?php echo url_for('ingresoReparaciones/agregarclientes')?>",
                                                {asynchronous:true, evalScripts:false,
                                                    onLoading:function(request, json){$('loader_agregar_tipo').show()},
                                                    parameters:Form.serialize(this)});
                                                //Hasta aca
                                                jQuery(this).dialog('close');
					}
				},
				Cancelar: function() {
					jQuery(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});

	});
	</script>




<div id="dialog-form" title="Crear un nuevo cliente">
	<p class="validateTips">Todos los campos son requeridos.</p>

        <?php echo form_remote_tag(array(
              'url' => url_for('ingresoReparaciones/agregarclientes'),
              'complete' => "jQuery('#dialog-form').dialog('close')",
        )) ?> 
        <?php //echo form_tag_for($form2, '@clientes', array('name' => 'clientes')) ?>
	<fieldset>
            <div style="display: none"><?php echo $form2['_csrf_token']?></div>
		<label for="name">Nombre</label>
		<!--<input type="text" name="name" id="name" class="text ui-widget-content ui-corner-all" />
                    <input type="text" name="apellido" id="Apellido" class="text ui-widget-content ui-corner-all" />
		    <input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all" />
		    <input type="text" name="direccion" id="direccion" value="" class="text ui-widget-content ui-corner-all" />
                -->
                <?php echo $form2['nombre']?>
                <label for="apellido">Apellido</label>
		<?php echo $form2['apellido'] ?>
                <label for="ruc">R.U.C</label>
		<?php echo $form2['ruc'] ?>

                <label for="tipo_cliente">Tipo de Cliente</label>
                <?php echo $form2['clientestipo_id']?>
                <label for="email">Email</label>
		<?php echo $form2['correo1'] ?>
                <label for="direccion">Dirección</label>
		<?php echo $form2['direccion'] ?>
	</fieldset>
	</form>
</div>


<button id="create-user" onclick="jQuery('#dialog-form').dialog('open')">Nuevo cliente</button>

